package com.cincc.cinccimdemo.ui.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.Constants;
import com.cincc.cinccimdemo.ui.me.favorite.FavoriteListActivity;

public class MineFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        initView(view);
        return view;
    }

    TextView nameTextView;
    TextView accountTextView;
    ImageView portraitImageView;


    private void initView(View view) {
        nameTextView = view.findViewById(R.id.nameTextView);
        nameTextView = view.findViewById(R.id.nameTextView);
        accountTextView = view.findViewById(R.id.accountTextView);
        portraitImageView = view.findViewById(R.id.portraitImageView);
        view.findViewById(R.id.meLinearLayout).setOnClickListener(view1 -> startMe());
        view.findViewById(R.id.favOptionItemView).setOnClickListener(view1 -> startFavActivity());

        refreshUI();
    }

    private void refreshUI() {
        nameTextView.setText(Constants.userInfo.nickName);
        accountTextView.setText("ID:" + Constants.userInfo.userId);

        Glide.with(this).load(Constants.userInfo.avatarUrl).placeholder(R.mipmap.avatar_def)
                .transforms(new CenterCrop(), new RoundedCorners(15))
                .into(portraitImageView);
    }

    private void startMe() {
        Intent i = new Intent(requireActivity(), MeActivity.class);
        startActivity(i);
    }

    private void startFavActivity() {
        Intent i = new Intent(requireActivity(), FavoriteListActivity.class);
        startActivity(i);
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshUI();
    }
}
